/*
 file: browsers.js
 dependsOn: core.js
 */

// Some temporary variables for calculating the flags below
var userAgent = navigator.userAgent.toLowerCase(),
    mSafari   = /mobile.*safari/.test(userAgent),
    webKitNdx = userAgent.indexOf('webkit/'),
    geckoNdx  = userAgent.indexOf('gecko/'),
    extraPP   = webKitNdx >= 0 || userAgent.indexOf('khtml/') >= 0,
    divProto  = document.createElement('div')['__proto__'],
    htmlEl    = !!window.HTMLElement;

// If our browser lacks the HTMLElement, see if we can add it 
if(divProto && !htmlEl) 
{
  window.HTMLElement = {};
  HTMLElement.prototype = divProto;
  htmlEl = true;
}

$.extend($, 
{
  /**
   * Flags whether the browser skips iterating over built-in
   * functions such as toString and valueOf
   * @alias $.BuiltInsHidden
   * @since 0.1
   */
  BuiltInsHidden: !('toString' in { toString: 1 }),

  /**
   * Flags whether the browser has built-in Element extensions
   * @alias $.ElementExtensions
   * @since 0.1
   */
  ElementExtensions: !mSafari && divProto && 
    divProto !== document.createElement('form')['__proto__'],

  /**
   * Flags whether the browser has a quirk with post parameters
   * @alias $.ExtraPostParam
   * @since 0.1
   */
  ExtraPostParam: extraPP,
  
  /**
   * Flags whether the browser is a version of Gecko
   * @alias $.Gecko
   * @since 0.1
   */
  Gecko: geckoNdx >= 0 && !extraPP,

  /**
   * Flags whether the browser defines the HTMLElement class
   * @alias $.HTMLElement
   * @since 0.1
   */
  HTMLElement: !!window.HTMLElement,
    
  /**
   * Flags whether the browser is a version of Internet Explorer
   * @alias $.IE
   * @since 0.1
   */
  IE: window.attachEvent && !window.opera,

  /**
   * Flags whether the browser is a version of mobile safari
   * @alias $.MSafari
   * @since 0.1
   */
  MSafari: mSafari,

  /**
   * Flags whether the browser is a version of Gecko that has
   * a few particular quirks
   * @alias $.OldGecko
   * @since 0.1
   */
  OldGecko: (geckoNdx >= 0 ? parseInt(
    userAgent.slice(geckoNdx + 5, geckoNdx + 9)) : NaN) < 2005,

  /**
   * Flags whether the browser is a version of Opera
   * @alias $.Opera
   * @since 0.1
   */
  Opera: !!window.opera,

  /**
   * Flags whether the browser provides a built-in querySelector
   * @alias $.SelectorsAPI
   * @since 0.1
   */
  SelectorsAPI: !!document.querySelector,

  /**
   * Flags whether the browser is a version of WebKit
   * @alias $.WebKit
   * @since 0.1
   */
  WebKit: webKitNdx >= 0,

  /**
   * Flags whether the browser provides an XMLHttpRequest object
   * @alias $.XHR
   * @since 0.1
   */
  XHR: !!window.XMLHttpRequest,

  /**
   * Flags whether the browser provides XPath support
   * @alias $.XPath
   * @since 0.1
   */
  XPath: !!document.evaluate
});

